package com.xhtt.modules.task.utils;

import com.alibaba.fastjson.JSON;
import com.xhtt.modules.cfg.entity.CfgHoseInfo;
import com.xhtt.modules.cfg.entity.CfgPumpInfo;
import com.xhtt.modules.cfg.entity.CfgTubeInfo;
import com.xhtt.modules.task.entity.TaskOperSeo;
import com.xhtt.modules.task.model.oper.LineModel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StaticUtils {
    public static int gettaskCount(String frestr, LocalDateTime startDate, LocalDateTime endDate) {
        int fre = Integer.valueOf(frestr.replace("h", ""));
        //根据作业时间和频率测算要ccr任务干的次数
        Duration dd = Duration.between(startDate, endDate);
        long hours = dd.toHours();
        if (0 != dd.toMinutes() % 60) {
            hours = hours + 1;
        }
        int count;
        if (fre >= hours) {
            count = 1;
        } else {
            count = (int) (hours / fre);
            if (hours % fre > 0) {
                count = count + 1;
            }
        }
        return count;
    }

    //11收料 12送料 13炭桶更换 14滤芯更换 15循环 16加热 17制冷 18打尺、取样 19VOCEC检测 20CCR记录 21管线清洗 99其他临时任务
    public static String getTempTaskName(Integer type) {
        if (11 == type.intValue()) {
            return "收料";
        }
        if (12 == type.intValue()) {
            return "送料";
        }
        if (13 == type.intValue()) {
            return "炭桶更换";
        }
        if (14 == type.intValue()) {
            return "滤芯更换";
        }
        if (15 == type.intValue()) {
            return "循环";
        }
        if (16 == type.intValue()) {
            return "加热";
        }
        if (17 == type.intValue()) {
            return "制冷";
        }
        if (18 == type.intValue()) {
            return "打尺、取样";
        }
        if (19 == type.intValue()) {
            return "VOCEC检测";
        }
        if (20 == type.intValue()) {
            return "CCR记录";
        }
        if (21 == type.intValue()) {
            return "管线清洗";
        }
        if (99 == type.intValue()) {
            return "其他临时任务";
        } else {
            return "未知临时任务";
        }

    }

    public static void setLine(TaskOperSeo seo) {
        if (StringUtils.isNotEmpty(seo.getLine())) {
            String line = "";
            LineModel lm = JSON.parseObject(seo.getLine(), LineModel.class);
            List<CfgHoseInfo> hoseList = lm.getHoseList();
            List<CfgTubeInfo> tubeList = lm.getTubeList();
            List<CfgPumpInfo> pumpList = lm.getPumpList();

            List<String> hoseNoList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(hoseList)) {
                hoseNoList = hoseList.stream().map(h -> h.getNumberNo()).collect(Collectors.toList());
            }
            List<String> tubeNoList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(tubeList)) {
                tubeNoList = tubeList.stream().map(t -> t.getNumberNo()).collect(Collectors.toList());
            }
            List<String> pumpNoList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(pumpList)) {
                pumpNoList = pumpList.stream().map(p -> p.getNumberNo()).collect(Collectors.toList());
            }
            hoseNoList.addAll(tubeNoList);
            hoseNoList.addAll(pumpNoList);
            if (CollectionUtils.isNotEmpty(hoseNoList)) {
                line = StringUtils.join(hoseNoList, "，");
            }
            seo.setLine(line);
        }
    }
}
